package com.dykj.fireFighting.web.base.system.controller;

import com.dykj.fireFighting.api.base.system.entity.SysDirectory;
import com.dykj.fireFighting.api.base.system.service.ISysDirectoryService;
import com.dykj.fireFighting.api.base.system.service.ISysUserService;
import com.dykj.fireFighting.api.base.system.vo.SysDirectoryVo;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;


/**
 * 数据字典接口
 *
 * @Author hxb
 * @Date 2021-06-21 10:54:44
 */
@Slf4j
@Api(value = "数据字典接口", tags = {"数据字典接口"})
@RestController
@RequestMapping("system/sysDirectory")
public class SysDirectoryController {

    @DubboReference
    private ISysDirectoryService sysDirectoryService;
    @DubboReference
    private ISysUserService sysUserService;


    /**
     * 数据字典树表
     *
     * @return
     */
    @ApiOperation(value = "日志列表", notes = "日志列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "value", value = "名称", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "code", value = "编码", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "dtyKeyCode", value = "编码", required = false, dataType = "String")
    })
    @GetMapping("/list")
    public CommonResult<List<SysDirectoryVo>> treeGrid(@RequestParam(required = false) String code,
                                                       @RequestParam(required = false) String value,
                                                       @RequestParam(required = false) String dtyKeyCode) {
        Map<String, Object> par = new HashMap<>();
        if (StringUtils.isNotBlank(code)) {
            par.put("code", StringUtils.deleteWhitespace(code));
        }
        if (StringUtils.isNotBlank(value)) {
            par.put("value", StringUtils.deleteWhitespace(value));
        }
        if (StringUtils.isNotBlank(dtyKeyCode)) {
            par.put("dtyKeyCode", StringUtils.deleteWhitespace(dtyKeyCode));
        }
        return CommonResult.success(sysDirectoryService.querySysDirectory(par));

    }

    /**
     * 获取数据字典信息
     */
    @AutoLog(value = "数据字典-获取数据字典信息")
    @ApiOperation(value = "获取数据字典信息", notes = "获取数据字典信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "Integer", required = true)
    @GetMapping("/info/{id}")
    public CommonResult<SysDirectoryVo> info(@PathVariable("id") Integer id) {
        SysDirectoryVo sysDirectoryVo = sysDirectoryService.getSysDirectoryVoById(id);
        return CommonResult.success(sysDirectoryVo);
    }

    /**
     * 保存数据字典信息
     */
    @AutoLog(value = "数据字典-保存数据字典信息")
    @ApiOperation(value = "保存数据字典信息", notes = "保存数据字典信息")
    @PostMapping("/save")
    public CommonResult save(@Valid @RequestBody SysDirectory sysDirectory) {
        try {
            String userId = Long.toString(ShiroUtil.getUser().getId());
            if (StringUtils.isBlank(sysDirectory.getParentCode())) {
                sysDirectory.setParentCode("-1");
            }
            sysDirectory.setCreateUser(userId);
            sysDirectory.setCreateTime(new Date());
            sysDirectoryService.save(sysDirectory);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改数据字典信息
     */
    @AutoLog(value = "数据字典-修改数据字典信息")
    @ApiOperation(value = "修改数据字典信息", notes = "修改数据字典信息")
    @PostMapping("/update")
    public CommonResult update(@Valid @RequestBody SysDirectory sysDirectory) {
        try {
            String userId = Long.toString(ShiroUtil.getUser().getId());
            sysDirectory.setUpdateUser(userId);
            sysDirectory.setUpdateTime(new Date());
            sysDirectoryService.updateById(sysDirectory);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除数据字典信息
     */
    @AutoLog(value = "数据字典-删除数据字典信息")
    @ApiOperation(value = "删除数据字典信息", notes = "删除数据字典信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "Integer", required = true, allowMultiple = true)
    @PostMapping("/delete")
    public CommonResult delete(@RequestBody Integer[] ids) {
        try {
            sysDirectoryService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 根据字典类型获取字典数据
     *
     * @return
     */
    @ApiOperation(value = "根据字典类型获取字典数据", notes = "根据字典类型获取字典数据")
    @GetMapping("/queryDataByType")
    public CommonResult<List<SysDirectory>> queryDataByType(String dtyKeyCode) {
        List<SysDirectory> sysDirectoryList = sysDirectoryService.queryDataByType(dtyKeyCode);
        return CommonResult.success(sysDirectoryList);
    }

    /**
     * 根据code获取字典数据
     * 默认获取用户区域
     *
     * @return
     */
    @ApiOperation(value = "根据code获取字典数据", notes = "根据code获取字典数据")
    @GetMapping("/queryDataByAreaCode")
    public CommonResult<List<SysDirectory>> queryDataByAreaCode(String code) {
        if (StringUtils.isBlank(code)) {
            code = ShiroUtil.getUser().getAreaCode();
        }
        List<SysDirectory> childSysDirectoryList = new ArrayList<SysDirectory>();
        SysDirectory sysDirectory = sysDirectoryService.queryByCode(code);
        childSysDirectoryList.add(sysDirectory);
        childSysDirectoryList = sysDirectoryService.getChildSysDirectoryList(childSysDirectoryList, code);
        return CommonResult.success(childSysDirectoryList);
    }

    /**
     * 根据字典类型获取字典数据
     *
     * @return
     */
    @ApiOperation(value = "根据字典类型获取字典数据(父级)", notes = "根据字典类型获取字典数据(父级)")
    @GetMapping("/queryParentDataByType")
    public CommonResult<List<SysDirectory>> queryParentDataByType(String dtyKeyCode) {
        List<SysDirectory> sysDirectoryList = sysDirectoryService.queryParentDataByType(dtyKeyCode);
        return CommonResult.success(sysDirectoryList);
    }

    /**
     * 根据code获取字典数据
     *
     * @return
     */
    @ApiOperation(value = "根据code获取字典数据", notes = "根据code获取字典数据")
    @GetMapping("/queryChildDataByCode")
    public CommonResult<List<SysDirectory>> queryChildDataByCode(String code) {
        List<SysDirectory> childSysDirectoryList = new ArrayList<SysDirectory>();
        if (StringUtils.isBlank(code)) {
            return CommonResult.success(childSysDirectoryList);
        }
        childSysDirectoryList = sysDirectoryService.getChildSysDirectoryList(childSysDirectoryList, code);
        return CommonResult.success(childSysDirectoryList);
    }
}
